angular.module('app').factory('R', ["$http", "$q", function($http, $q){
    var _trans = function(method, url, data, still){
        var deferred = $q.defer();

        var obj = {
            method: method,
            url: 'http://xc.arinchina.com'+url
        };

        if (method == 'GET') obj.params = data;
        if (method == 'POST') obj.data = {
            'data': data
        };
        
        $http(obj).then(function(data){
            var data = data.data;
            if (data.rtnCode == 0000000 || still){
                deferred.resolve(data);
            }else{
                console.error(data.msg);
            }
        }, function(){
            console.error('网络出问题了，请联系管理员');
        });
        return deferred.promise;
    }
    return {
        getFileUpload: function(){
            return 'http://xc.arinchina.com/ar/common/uploadFile.do';
        },
        getTime: function(str){
            var a = new Date(str);
            return a.getTime();
        },
        getTimeFormat: function(date, fmt){
            var date = new Date(date);
            var o = {
                "M+": date.getMonth() + 1, //月份 
                "d+": date.getDate(), //日 
                "h+": date.getHours(), //小时 
                "m+": date.getMinutes(), //分 
                "s+": date.getSeconds(), //秒 
                "q+": Math.floor((date.getMonth() + 3) / 3), //季度 
                "S": date.getMilliseconds() //毫秒 
            };
            if (/(y+)/.test(fmt)) fmt = fmt.replace(RegExp.$1, (date.getFullYear() + "").substr(4 - RegExp.$1.length));
            for (var k in o)
            if (new RegExp("(" + k + ")").test(fmt)) fmt = fmt.replace(RegExp.$1, (RegExp.$1.length == 1) ? (o[k]) : (("00" + o[k]).substr(("" + o[k]).length)));
            return fmt;
        },
        getDemandDetailById : function(data){
            var url = "/ar/common/getDemandDetailById.do", method = "GET";
            return _trans(method, url, data);
        },
        getProductDetailById : function(data){
            var url = "/ar/common/getProductDetailById.do", method = "GET";
            return _trans(method, url, data);
        },
        getProductDetailById : function(data){
            var url = "/ar/common/getProductDetailById.do", method = "GET";
            return _trans(method, url, data);
        },
        getAllClassifyAndDirection : function(data){
            var url = "/ar/common/getAllClassifyAndDirection.do", method = "GET";
            return _trans(method, url, data);
        },
        getAddressList : function(data){
            var url = "/ar/common/getAddressList.do", method = "GET";
            return _trans(method, url, data);
        },
        getRecomandIndex : function(data){
            var url = "/ar/common/getRecomandIndex.do", method = "GET";
            return _trans(method, url, data);
        },
        getIndexProducts : function(data){
            var url = "/ar/web/product/getIndexProducts.do", method = "GET";
            return _trans(method, url, data);
        },
        getProductList : function(data){
            var url = "/ar/web/product/getProductList.do", method = "GET";
            return _trans(method, url, data);
        },
        getProductDetailById : function(data){
            var url = "/ar/common/getProductDetailById.do", method = "GET";
            return _trans(method, url, data);
        },
        getIndexDemands : function(data){
            var url = "/ar/web/demand/getIndexDemands.do", method = "GET";
            return _trans(method, url, data);
        },
        getDemandList : function(data){
            var url = "/ar/web/demand/getDemandList.do", method = "GET";
            return _trans(method, url, data);
        },
        login : function(data){
            var url = "/ar/web/user/login.do", method = "POST";
            return _trans(method, url, data, 1);
        },
        loginOut : function(data){
            var url = "/ar/web/user/loginOut.do", method = "GET";
            return _trans(method, url, data);
        },
        getGraphCode : function(data){
            var url = "/ar/common/getGraphCode.do", method = "GET";
            return _trans(method, url, data, 1);
        },
        sendSmsCode : function(data){
            var url = "/ar/common/sendSmsCode.do", method = "POST";
            return _trans(method, url, data, 1);
        },
        commonRegist : function(data){
            var url = "/ar/web/user/commonRegist.do", method = "POST";
            return _trans(method, url, data, 1);
        },
        perfectUserInfo : function(data){
            var url = "/ar/web/user/perfectUserInfo.do", method = "POST";
            return _trans(method, url, data);
        },
        publishDemand : function(data){
            var url = "/ar/web/demand/publishDemand.do", method = "POST";
            return _trans(method, url, data, 1);
        },
        publishProduct : function(data){
            var url = "/ar/web/product/publishProduct.do", method = "POST";
            return _trans(method, url, data, 1);
        },
        getUserInfoById : function(data){
            var url = "/ar/web/user/getUserInfoById.do", method = "GET";
            return _trans(method, url, data);
        },
        updateUserInfo : function(data){
            var url = "/ar/web/user/updateUserInfo.do", method = "POST";
            return _trans(method, url, data);
        },
        getDemandsByUserId : function(data){
            var url = "/ar/web/demand/getDemandsByUserId.do", method = "GET";
            return _trans(method, url, data);
        },
        getProductsByUserId : function(data){
            var url = "/ar/web/product/getProductsByUserId.do", method = "GET";
            return _trans(method, url, data);
        },
        getMsgsByUserId : function(data){
            var url = "/ar/web/msg/getMsgsByUserId.do", method = "GET";
            return _trans(method, url, data);
        },
        getAllClassify : function(data){
            var url = "/ar/common/getAllClassify.do", method = "GET";
            return _trans(method, url, data);
        },
        sendApplyIntention : function(data){
            var url = "/ar/web/msg/sendApplyIntention.do", method = "POST";
            return _trans(method, url, data);
        },
        checkApplyUserList : function(data){
            var url = "/ar/web/msg/checkApplyUserList.do", method = "GET";
            return _trans(method, url, data);
        },
        getTicketMap : function (data) {
            var url = "/ar/web/wx/getTicketMap.do", method = "POST";
            return _trans(method, url, data);
        }
    }
}]);
